package com.bitcola.chainserver.chain.trx.rest;


import com.bitcola.chainserver.chain.trx.api.TransactionResult;
import com.bitcola.chainserver.chain.trx.config.Constant;
import com.bitcola.chainserver.chain.trx.core.Credential;
import com.bitcola.chainserver.chain.trx.core.Trc20;
import com.bitcola.chainserver.chain.trx.core.TronApi;
import com.bitcola.chainserver.chain.trx.core.TronKit;
import com.bitcola.chainserver.chain.trx.service.AddressService;
import com.bitcola.chainserver.chain.trx.service.TransactionScanner;
import com.bitcola.chainserver.chain.trx.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class ApiController {

    @Autowired
    TransactionScanner transactionScanner;
    @Autowired
    AddressService addressService;
    @Autowired
    TransactionService service;


    //增加监听 的地址

    @RequestMapping(value = "addAddress")
    public String addAddress(@RequestBody List<String> address) {
        transactionScanner.addAddress(address);
        return "ok";
    }
    //增加监听的合约地址
    @RequestMapping(value = "addContractAddress")
    public String addContractAddress(@RequestBody List<String> address) {
        transactionScanner.addContractAddress(address);
        return "ok";
    }

    //生成新的地址,返回 地址,公钥 私钥
    @RequestMapping(value = "newAddress")
    public Map newAddress() {
        return addressService.newAddress();
    }
    @RequestMapping(value = "scanBlock", method = RequestMethod.GET)
    public String scanBlock(long blockNumber) throws Exception {
        var tronAPi = new TronApi();
        var list= tronAPi.getTransformsByBlockNumber(blockNumber);
        transactionScanner.scanBlock(list);
        return "ok";
    }
    //转账TRX
    @RequestMapping(value = "sendTrx")
    public TransactionResult sendTrx(String privateKey, String toAddress, BigDecimal value) throws Exception {
        return service.transformTrx(privateKey,toAddress,value.longValue()* Constant.SUN);
    }

    //转账trc20代币
    @RequestMapping(value = "sendTrc20")
    public TransactionResult sendTrc20(String privateKey, String toAddress, BigDecimal value,String contractAddress) throws Exception {
        return service.transformToken(privateKey,toAddress,value.longValue(),contractAddress);
    }


    @RequestMapping(value = "getBalance")
    public BigDecimal getBalance(String address,String contractAddress) throws Exception {
        return addressService.getBalance(address,contractAddress);

    }

    @RequestMapping(value="getBlockNumber")
            public long getBlockNumber()
    {
        return transactionScanner.getLastBlockNumber();

    }
    @RequestMapping(value="getTransactionByHash")
    public Map getTransactionByHash(String hash) throws Exception {
        return service.getTransactionById(hash);

    }

    @RequestMapping(value = "getTransactionsByBlockNumber")
    public Map getTransactionsByBlockNumber(long blockNumber) throws Exception {
        TronApi tronApi=new TronApi();
        var tokenList = tronApi.getBlockEvents(blockNumber);
        tokenList = tokenList.stream().filter(x -> x.eventName.equals("Transfer")).collect(Collectors.toList());

        var transactionList=tronApi.getTransformsByBlockNumber(blockNumber);

        Map<String ,Object>result=new HashMap<>(4);

        result.put("tokens",tokenList);
        result.put("transactions",transactionList);
        return result;


    }
    @RequestMapping(value = "getTokenDecimal")

    public long getTokenDecimal(String contractAddress) throws Exception {
          TronKit kit = new TronKit(
            TronApi.mainNet(),
            Credential.fromPrivateKey("8D9142B97B38F992B4ADF9FB3D0DD527B1F47BE113C6D0B5C32A0571EF1E7B5F")
    );


        Trc20 token = kit.trc20(contractAddress);
        return token.decimals().longValue();
    }



}
